package cn.fufeiii.bt.nomiddleware.openfeign.codec;

import cn.fufeiii.bt.common.util.result.BizException;
import cn.hutool.core.text.StrPool;
import feign.Response;
import feign.codec.ErrorDecoder;
import lombok.extern.slf4j.Slf4j;


/**
 * 简单的错误编码器
 *
 * @author FuFei
 */
@Slf4j
public class SimpleErrorDecoder implements ErrorDecoder {

    /**
     * 错误编码器
     *
     * @see feign.codec.ErrorDecoder.Default
     * @see feign.FeignException.FeignExceptionMessageBuilder#build()
     * @see feign.AsyncResponseHandler#handleResponse(java.util.concurrent.CompletableFuture, String, Response, java.lang.reflect.Type, long)
     */
    @Override
    public Exception decode(String methodKey, Response response) {
        String result = String.format("[%d] during [%s] to [%s]", response.status(), response.request().httpMethod(), response.request().url());
        if (log.isDebugEnabled()) {
            log.debug(result + StrPool.C_SPACE + methodKey);
        }
        return new BizException(result);
    }


}
